package com.southpark.system.managers
{
	import com.southpark.system.interfaces.managers.IApplicationManager;
	import com.southpark.system.interfaces.managers.IDeepLinkingManager;
	
	import flash.utils.Dictionary;
	
	import mx.events.BrowserChangeEvent;
	import mx.managers.BrowserManager;
	import mx.managers.IBrowserManager;
	

	
	public class DeepLinkingManager implements IDeepLinkingManager
	{	
		
		public function DeepLinkingManager(applicationManager:IApplicationManager)
		{
			this.applicationManager = applicationManager;
			createPageDictionaries();
		}
		
		/**
		 * 
		 */
		private var pageDictionaryByInterface:Dictionary;
		private var pageDictionaryById:Dictionary;
		
		private function createPageDictionaries():void
		{
			pageDictionaryByInterface = new Dictionary();
			pageDictionaryById = new Dictionary();
			
			for each (var item:Array in applicationManager.pageMap)
			{
				definePage(item[0],item[2]);
			};
		} 
		
		private function definePage(pageInterface:Class,pageId:String):void
		{	
		 	pageDictionaryByInterface[pageInterface] = pageId;
		 	pageDictionaryById[pageId] = pageInterface;
		}
		
		
		/**
		 * 
		 */
		private const DEEP_LINKING_SEPARATOR:String = "/"; 
		
		private const PAGE:String = "page";
		private const ID:String = "id";
		
		
		private var browserManager:IBrowserManager;
		private var applicationManager:IApplicationManager;
		
		
		public function start():void
		{
			if (!browserManager)
			{
				browserManager = BrowserManager.getInstance();
				browserManager.init();
				browserManager.addEventListener(BrowserChangeEvent.BROWSER_URL_CHANGE,parseDeepLinking);
			};
			
			parseDeepLinking();
		}
		
		 
		private function parseDeepLinking(event:BrowserChangeEvent=null):void
        {
            var fragment:Object = getBrowserFragment();
            
  			//temp
  			//fragment = {page:"builder"};

            
            if (!fragment || fragment[PAGE] == undefined)
            {
            	applicationManager.pageManager.addPage(applicationManager.defaultPageClass);
            }
            else
            {
            	applyDeepLinking(fragment);
            };
        }
        
        private function applyDeepLinking(fragment:Object):void
        {
        	var pageInterface:Class = pageDictionaryById[fragment[PAGE]];
        	if (pageInterface) applicationManager.pageManager.addPage(pageInterface);
        }
		

        public function updateDeepLinking():void
        {
            var fragment:Object = new Object();        
        	fragment[PAGE] = pageDictionaryByInterface[applicationManager.pageManager.selectedPageInterface];

        	var fragmentToString:String = objectToString(fragment);   	
        	browserManager.setFragment(fragmentToString);
        	
        	browserManager.setTitle(applicationManager.pageManager.selectedPageViewUI.title);
        }
        
        public function openLink(link:String):void
        {
        	applyDeepLinking(stringToObject(link));
        }
        
         /**
        * 
        */
        private function getBrowserFragment():Object
        {
         	var fragment:Object;
            
            try
            {
                fragment = stringToObject(browserManager.fragment);
        	} 
        	catch (e:Error) 
        	{
        		
        	};
        	
        	return fragment;
     	}
        
        /**
        * 
        */
        private function objectToString(object:Object):String
        {
			var string:String = "";
			
			if (object.hasOwnProperty(PAGE))
			{
				string += DEEP_LINKING_SEPARATOR + object[PAGE];
			};
			
			if (object.hasOwnProperty(ID))
			{
				string += DEEP_LINKING_SEPARATOR + object[ID];
			};
			
			return string;
        }
        
        private function stringToObject(string:String):Object
        {
        	var array:Array = string.split(DEEP_LINKING_SEPARATOR);
        	var object:Object = new Object();
        	
        	
        	if (array.length <= 1) return null;
        	
        	object[PAGE] = array[1];
        	
        	if (array.length > 2)
        	{
        		object[ID] = array[2];
        	};
        	
        	return object;
        }
		
		
	}
}